package com.gitee.urick.union.builder;

import com.gitee.urick.union.config.UnionConfig;
import com.gitee.urick.union.template.UnionDocBuilderTemplate;
import com.power.common.util.DateTimeUtil;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.model.ApiDoc;
import com.power.doc.template.IDocBuildTemplate;
import com.thoughtworks.qdox.JavaProjectBuilder;

import java.util.List;

import static com.gitee.urick.union.constants.UnionConstants.UNION_ALL_IN_ONE_MD_TPL;
import static com.gitee.urick.union.constants.UnionConstants.UNION_OUT_MD_DIR;

/**
 * @author Rick 2021/02/08
 * @since 1.0
 */
public class UnionMarkdownBuilder {
    private static final String DATE_FORMAT = "yyyyMMddHHmm";

    /**
     * @param config ApiConfig
     */
    public static void buildApiDoc(UnionConfig config) {
        JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
        buildApiDoc(config, javaProjectBuilder);
    }

    /**
     * Only for smart-doc maven plugin and gradle plugin.
     *
     * @param config             ApiConfig
     * @param javaProjectBuilder ProjectDocConfigBuilder
     */
    public static void buildApiDoc(UnionConfig config, JavaProjectBuilder javaProjectBuilder) {
        config.setAdoc(false);
        config.setHtml(false);
        config.setShowJavaType(true);
        UnionDocDataBuilder builderTemplate = new UnionDocDataBuilder();
        builderTemplate.checkAndInit(config);
        ProjectDocConfigBuilder configBuilder = new ProjectDocConfigBuilder(config, javaProjectBuilder);
        IDocBuildTemplate<ApiDoc> docBuildTemplate = new UnionDocBuilderTemplate();
        List<ApiDoc> apiDocList = docBuildTemplate.getApiData(configBuilder);
        if (config.isAllInOne()) {
            String version = config.isCoverOld() ? "" : "-V" + DateTimeUtil.long2Str(System.currentTimeMillis(), DATE_FORMAT);
            builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, UNION_OUT_MD_DIR + UNION_ALL_IN_ONE_MD_TPL, "API" + version + ".md");
        } else {
            builderTemplate.buildSingleDoc(apiDocList, config, javaProjectBuilder, UNION_OUT_MD_DIR);
        }
    }
}
